/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2021并保留所有权利。
 * Copyright 2021, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.aqs.test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo {
    private static final Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                test();
            }
        },"线程1").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                test();
            }
        },"线程2").start();
    }

    public static void test() {
        try {
            //获取锁
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "获取到锁了");
            //业务代码,使用部分花费100毫秒
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            //释放锁放在finally中。
            lock.unlock();
            System.out.println(Thread.currentThread().getName() + "释放了锁");
        }
    }
}